package com.coda.controller.user;

import com.coda.common.Result;
import com.coda.constant.MessageConstant;
import com.coda.context.BaseContext;
import com.coda.pojo.Comment;
import com.coda.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户评论controller
 */
@RestController
@RequestMapping("/user/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 发表评论
     */
    @PutMapping
    public Result addComment(@RequestBody Comment comment) {
        comment.setUserId(BaseContext.getCurrentId());
        comment.setCreateTime(LocalDateTime.now());
        int result = commentService.addComment(comment);
        return result > 0 ? Result.success() : Result.error(MessageConstant.UPVOTE_ERR);
    }

    /**
     * 查看所有用户评论
     */
    @GetMapping
    public Result<List<Comment>> getCommentAll(String orderType) {
        List<Comment> commentList = commentService.getCommentAll(orderType);
        return Result.success(commentList);
    }

    /**
     * 点赞
     */
    @PostMapping("/{id}")
    public Result upvote(@PathVariable Integer id) {
        int result = commentService.upvote(id);
        return result > 0 ? Result.success() : Result.error(MessageConstant.UPVOTE_ERR);
    }
}
